package com.fire.eyes.controller;

import com.fire.eyes.service.IGoodsInfoService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.fire.core.base.controller.BaseController;
import com.fire.core.service.ITMitErrorCodeService;
import com.fire.core.util.AjaxResult;
import com.fire.core.util.BlogUtil;
import com.fire.eyes.constants.ErrorCodeConstants;
import com.fire.eyes.service.ITMitChannelService;
/**
 * 类描述：商品信息接口
 * 
 * @author: Aspire
 * @version 1.0
 */
@RestController
@RequestMapping("/goods")
public class GoodsInfoController extends BaseController{
    private static Logger logger = Logger.getLogger(GoodsInfoController.class);
    @Autowired
    private IGoodsInfoService goodsService;
    @Autowired
	private ITMitErrorCodeService errorCodeService;
    @Autowired
    private ITMitChannelService itChannelService;
    
    
    /**
     * 2.1.1 查询商品货架或者主题
     * @param json
     * @return
     */
    @RequestMapping(value = "/getshelf", method = RequestMethod.POST)
    public AjaxResult getShelf(@RequestBody String json) {
    	logger.info("查询商品货架或者主题接口转换到的消息" + json);
    	BlogUtil.inlog(json, "", "mit/goods/getshelf", "getshelf", "req");
    	AjaxResult ajaxResult = new AjaxResult();
    	try {
            ajaxResult = goodsService.queryGoodShelfInfo(json);
            String resJSON = JSON.toJSONString(ajaxResult);
            logger.info("商品货架或者主题响应消息:"+resJSON);
            BlogUtil.inlog(resJSON, "", "mit/goods/getshelf", "getshelf", "resp");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("查询商品货架或者主题接口调用出错：",e);
            ajaxResult.setStatus(ErrorCodeConstants.INTERNAL_ERROR);
            ajaxResult.setMessage("系统异常");
            ajaxResult.setTips("系统异常");
        }
        return errorCodeService.replaceMess(ajaxResult);
    }
    
    /**
     * 2.1.8	获取banner接口
     * @param json
     * @return
     */
    @RequestMapping(value = "/getbanner", method = RequestMethod.POST)
    public AjaxResult getBanner(@RequestBody String json) {
        
        logger.info("获取banner接口请求消息" + json);
        BlogUtil.inlog(json, "", "mit/goods/getbanner", "getbanner", "req");
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = goodsService.getBannerList(json);
            String resJSON = JSON.toJSONString(ajaxResult);
            logger.info("获取banner响应消息:"+resJSON);
            BlogUtil.inlog(resJSON, "", "mit/goods/getbanner", "getbanner", "resp");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取banner接口调用出错：",e);
            ajaxResult.setStatus(ErrorCodeConstants.INTERNAL_ERROR);
            ajaxResult.setMessage("系统异常");
            ajaxResult.setTips("系统异常");
        }
        return errorCodeService.replaceMess(ajaxResult);
    }
    
    /**
	 * 查询渠道接口
	 * @param json
	 * @return
	 */
	@PostMapping(value = "/getchannel")
	@ResponseBody
	public AjaxResult queryChannel(@RequestBody String json){
		logger.info("========查询渠道开始========");
		logger.info("查询渠道请求消息:"+json);
		BlogUtil.inlog(json, "", "mit/goods/getchannel", "getchannel", "req");
		AjaxResult ajaxResult = new AjaxResult();
		try {
			ajaxResult=itChannelService.queryChannel(json);
		} catch (Exception e) {
			logger.error("查询渠道接口错误===>" + e.getMessage());
			e.printStackTrace();
			ajaxResult.setContext(null);
			ajaxResult.setStatus(ErrorCodeConstants.INTERNAL_ERROR);
			ajaxResult.setMessage("系统异常");
			String tips=errorCodeService.getTips(ErrorCodeConstants.INTERNAL_ERROR);
			if(StringUtils.isNotBlank(tips)){
				ajaxResult.setTips(tips);
			}else{
				ajaxResult.setTips("["+ErrorCodeConstants.INTERNAL_ERROR+"]"+"系统异常");
			}
		}
		String jsonString = JSON.toJSONString(ajaxResult);
		logger.info("查询渠道响应消息:"+jsonString);
		BlogUtil.inlog(jsonString, "", "mit/goods/getchannel", "getchannel", "resp");
		logger.info("========查询渠道结束========");
		return ajaxResult;
		    	
	 }
}

